function nrExtractResources(ind, grids...; )
    # 预分配输出
    opts=Dict{Any,Any}("IndexStyle" => "index", "IndexBase" => "1based", "ExtractionMethod" => "allplanes")
    ngrids = length(grids)
    out = Vector{Any}(undef, 2 * ngrids)
    # 索引格式处理
    if opts["IndexBase"] == "0based"
        ind = ind .+ 1  # 0-based 转 1-based
    end
    # 遍历每个资源数组
    for i in 1:ngrids
        grid = grids[i]  # 提取当前资源数组
        gSize = size(grid)
        M = gSize[1]  # 子载波数
        N = gSize[2]  # OFDM 符号数

        # 根据提取方法确定投影维度
        if opts["ExtractionMethod"] == "allplanes"
            projDims = (size(grid, 4), size(grid, 3))
        else
            projDims = size(grid, 3)
        end

        # 索引验证
        if !all(ind .> 0) || !all(isfinite.(ind))
            error("Indices must be positive and finite.")
        end

        if opts["IndexStyle"] == "subscript"
            if size(ind, 2) != 3
                error("Subscript indices must have 3 columns.")
            end
        end

        # 索引转换
        if opts["IndexStyle"] == "subscript"
            P = maximum(ind[:, 3])
            subIndTmp = ind  # [M, N, P] 索引
            linInd = sub2ind((M, N, P), [ind[:, 1], ind[:, 2], ind[:, 3]])
        else 
            P = maximum(ceil.(Int, ind[:] / (M * N)))
            linInd = ind
            subM, subN, subP =ind2sub((M, N, P),linInd[:])
            subIndTmp = hcat(subM, subN, subP)  # [M, N, P] 索引
        end

        if isempty(P)
            P = 1  # 如果没有检测到平面，则强制 P = 1
        end

        # 直接提取方法
        if ndims(grid) < 4 && opts["ExtractionMethod"] == "direct"
            indOut = ind
        else
            # 投影索引
            if opts["ExtractionMethod"] == "allplanes"
                subIndIn = unique(subIndTmp[:, 1:2], dims=1) 
            else
                subIndIn = subIndTmp
            end
            indPerR = size(subIndIn, 1)  # 每个接收天线的索引数

            # Index of first dimension projected
            projStartIdx = size(subIndIn, 2) + 1
            subIndOut = hcat(subIndIn, zeros(indPerR, length(projDims)))


            if opts["ExtractionMethod"] == "direct" && ndims(grid) > 3
                # Direct mode: 确定每个平面的索引数，如果不同则返回列向量
                indPerP = sum(repeat(1:P, 1, indPerR)' .== repeat(subIndOut[:, 3], 1, P), dims=1)
                if maximum(indPerP) - minimum(indPerP) == 0
                    # NRE-by-R-by-P
                    sizeOut = [indPerR / P, P, projDims...]
                    # 交换第2和第3维度
                    sizeOut[[2 3]] = sizeOut[[3 2]]
                else
                    # NNRETotal-by-1-by-1
                    sizeOut = [indPerR * prod(projDims), 1, 1]
                end
            else
                # Allplanes mode: 每个平面的元素数相同
                sizeOut = [indPerR, projDims..., 1]
                # 交换第2和第3维度
                sizeOut[[2 3]] = sizeOut[[3 2]]
            end


            # 输出索引生成
            if opts["ExtractionMethod"] == "allplanes"
                linInd = sub2ind((size(grid, 1), size(grid, 2)), [subIndIn[:, 1], subIndIn[:, 2]])
                linIndExtended = hcat(linInd, linInd .+ ( (1:prod(projDims)-1) * size(grid, 1) * size(grid, 2))' )
                linInd = reshape(linIndExtended, (size(subIndIn, 1), projDims[2], projDims[1]))

                if opts["IndexStyle"] == "subscript"
                    # Generate subIndOut only if needed
                    col1, col2, col3, col4 = ind2sub(size(grid), linInd[:])
                    subIndOut = hcat(col1, col2, col3, col4)
                end
            else
                # 投影索引到接收天线和高维
                subIndOut = repeat(subIndIn, prod(projDims), 1)
                addedInd = repeat(1:prod(projDims), indPerR)
                subR = ind2sub(projDims, addedInd[:])
                subIndOut[:, projStartIdx] = subR

                # 重新排序为 M-by-N-by-R-by-P
                subIndOut[:, [3, 4]] = subIndOut[:, [4, 3]]

                # 按维度升序排序（忽略下标索引 1 和 2）
                sortIdx = sort(subIndOut[:, end:-1:3], dims=1)
                subIndOut = subIndOut[sortIdx, :]

                # 转换为线性索引并重塑
                linInd = reshape(sub2ind(size(grid), [subIndOut[:, 1], subIndOut[:, 2], subIndOut[:, 3], subIndOut[:, 4]]), (sizeOut[1:3]...))
            end

            # 输出线性或下标索引
            if opts["IndexStyle"] == "subscript"
                indOut = subIndOut[:, 1:ndims(grid)]
            else
                indOut = linInd
            end
        end

        # 验证线性索引
        nElements = length(grid)
        if any(linInd[:] .> nElements)
            error("Index exceeds grid dimensions.")
        end

        # 提取资源元素
        out[i] = grid[linInd]

        # 将索引转换回原始格式
        if opts["IndexBase"] == "0based"
            indOut = indOut .- 1
        end

        # 返回索引
        out[i + ngrids] = indOut
    end

    # 返回结果
    return out
end

function ind2sub(shape, indices)
    n = length(indices)
    d = length(shape)
    cndx = CartesianIndices(Dims(shape))
    out = Array{Vector{Int64}}(undef, d)
    for i = 1:d
        a= Array{Int64}(undef, n)
        for j in 1:n
            lndx = indices[j]
            a[j] = cndx[lndx][i]
        end
        out[i]=a
    end
    return out
end

function sub2ind(shape, cindices)

    lndx = LinearIndices(Dims(shape))
    d = length(cindices)
    n = length(cindices[1])
    out = Array{Int}(undef, n)
    for i = 1:n
        a = Int[]
        for j = 1:d
            push!(a, cindices[j][i])
        end
        out[i] = lndx[a...]
    end
    return out
end


# using MAT
# data_file = matread("E:\\学位打工仔\\julia转写\\5G\\code\\allW.mat")
# allW=data_file["allW"]

# ind=[1,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
# ueW,~=nrExtractResources(ind,allW)
# matwrite("ueWs.mat", Dict("ueWs" => ueW))